package com.gsdkjgpt4.sys.controller;

import com.gsdkjgpt4.common.vo.Result;
import com.gsdkjgpt4.sys.entity.User;
import com.gsdkjgpt4.sys.service.IUserService;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author rendong
 * @since 2024-01-18
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/all")
    public Result<List<User>> getAlluser() {
        List<User> list = userService.list();
        return Result.success(list, "查询成功");
    }

    //登录接口
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody User user) {
        Map<String, Object> data = userService.login(user);

        if (data != null) {
            return Result.success(data);
        }
        return Result.fail(0, "用户名或密码错误");
    }

    //通过token查询用户信息
    @GetMapping("/info")
    public Result<?> getUserInfo(@RequestParam("token") String token) {
        Map<String, Object> data = userService.getUserInfo(token);
        if (data != null) {
            return Result.success(data);
        }
        return Result.fail(0, "用户信息获取失败");
    }

    //通过用户ID查询用户信息
    @PostMapping("/selectByUserId")
    public Result<?> selectByUserId(@RequestBody User user) {
        Map<String, Object> data = userService.selectByUserId(user.getUserId());
        if (data != null) {
            return Result.success(data);
        }
        return Result.fail(0, "用户信息获取失败");
    }

    //修改用户姓名和密码
    @PostMapping("/updateUserName")
    public Result<?> updateUser(@RequestBody User user) {
        Map<String, Object> data = userService.updateUserName(user.getUserId(), user.getUserName(), user.getPassWord());
        if (data != null) {
            return Result.success();
        }
        return Result.fail(0, "修改失败");
    }

    @PostMapping("/updatePosition")
    public Result<?> updatePosition(@RequestBody User user) {
        Map<String, Object> data = userService.updatePosition(user.getUserId());
        if (data == null) {
            return Result.fail("修改错误！");
        }
        return Result.success("修改成功");
    }

    @PostMapping("/updateLimitedNumber")
    public Result<?> updateLimitedNumber(@RequestBody User user) {
        Map<String, Object> data = userService.limitedNumber(user.getUserId(),user.getLimitedNumber());
        if (data == null) {
            return Result.fail("修改错误！");
        }
        return Result.success("修改成功");
    }

    @PostMapping("/signUp")
    public Result<?> signUp(@RequestBody User user){
        Map<String, Object> data = userService.signUp(user.getUserName(),user.getPassWord());
        if (data == null) {
            return Result.fail("注册错误！");
        }else if(data.get("result") == "repeat"){
            return Result.fail(-1,"用户名重复");
        }
        return Result.success("注册成功");
    }
}
